
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'home/home.dart';
import 'mine/me.dart';

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<StatefulWidget> createState() {
    return _AppState();
  }
}

class _AppState extends State<App> {
  final appBarTitles = ['首页', '我的'];
  final tabTextStyleSelected = const TextStyle(color: Color(0xff3B9AFF));
  final tabTextStyleNormal = const TextStyle(color: Color(0xff969696));
  static const Color defaultColor =  Color(0xFF3B9AFF);
  int _tabIndex = 0;
  var tabImages;
  var _body;
  var pages;

  Image getTabImage(path) {
    debugPrint("getTabImage $path");
    return Image.asset(path, width: 20.0, height: 20.0);
  }

  @override
  void initState() {
    super.initState();
    pages = <Widget>[const Home(), const Me()];
    tabImages ??= [
        [
          getTabImage('assets/images/tabBar/index.png'),
          getTabImage('assets/images/tabBar/index1.png')
        ],
        [
          getTabImage('assets/images/tabBar/my.png'),
          getTabImage('assets/images/tabBar/my1.png')
        ]
      ];
  }

  TextStyle getTabTextStyle(int curIndex) {//设置tabbar 选中和未选中的状态文本
    if (curIndex == _tabIndex) {
      return tabTextStyleSelected;
    }
    return tabTextStyleNormal;
  }

  Image getTabIcon(int curIndex) {//设置tabbar选中和未选中的状态图标
    if (curIndex == _tabIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }

  Text getTabTitle(int curIndex) {
    return Text(appBarTitles[curIndex], style: getTabTextStyle(curIndex));
  }

  @override
  Widget build(BuildContext context) {
    _body = IndexedStack(
      children: pages,
      index: _tabIndex,
    );
    return MaterialApp(
      theme: ThemeData(
          primaryColor: defaultColor
      ),
      home: Scaffold(//布局结构
        appBar: AppBar(//选中每一项的标题和图标设置
            title: Text(appBarTitles[_tabIndex],
                style: const TextStyle(color: Colors.white)),
            backgroundColor: defaultColor,
            iconTheme: const IconThemeData(color: Colors.white)
        ),
        body: _body,
        bottomNavigationBar: CupertinoTabBar(//
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: getTabIcon(0),
                label: appBarTitles[0]
            ),
            BottomNavigationBarItem(
                icon: getTabIcon(1),
                label: appBarTitles[1]
            )
          ],
          currentIndex: _tabIndex,
          onTap: (index) {
            setState((){
              _tabIndex = index;
            });
          },
        ),
         // drawer: MyDrawer()
      ),
    );
  }
}

